import requests
from ..shippypro import api_key, headers, shippypro_url
import json

key = api_key
headers = headers
url = shippypro_url


class PickupAPI(object):
    """ 上门取件接口 """

    @classmethod
    def book_pickup(cls,
                    send_name: str,
                    send_company_name: str,
                    send_phone: str,
                    send_email: str,
                    send_country: str,
                    send_state: str,
                    send_city: str,
                    send_street: str,
                    send_postal_code: str,
                    receive_country: str,
                    parcels_list: list,
                    courier_name: str,
                    courier_service_code: int,
                    book_timestamp: int,
                    book_note: str,
                    ) -> dict:
        """
        预约上门取件
        :param send_name: 发件人名称
        :param send_company_name: 发件人公司名称
        :param send_phone: 发件人电话
        :param send_email: 发件人邮箱
        :param send_country: 发件人国家代码
        :param send_state: 发件人省份
        :param send_city: 发件人城市
        :param send_street: 发件人街道
        :param send_postal_code: 发件人邮编
        :param receive_country: 收件人国家代码
        :param parcels_list: 包裹集合[{"length": 5,"width": 5,"height": 5,"weight": 10}]
        :param courier_name: 承运商名称
        :param courier_service_code: ShipPyPro中承运商服务编码
        :param book_timestamp:预约时间（时间戳）
        :param book_note:预约描述
        """
        values = {
            "Method": "BookPickup",
            "Params": {
                "to_address": {
                    "country": receive_country
                },
                "from_address": {
                    "name": send_name,
                    "company": send_company_name,
                    "street1": send_street,
                    "street2": "",
                    "city": send_city,
                    "state": send_state,
                    "zip": send_postal_code,
                    "country": send_country,
                    "phone": send_phone,
                    "email": send_email
                },
                "parcels": parcels_list,
                "CarrierName": courier_name,
                "CarrierID": courier_service_code,
                "PickupTime": book_timestamp,
                "PickupNote": book_note,
                "PickupMorningMintime": "09:00",
                "PickupMorningMaxtime": "12:00",
                "PickupAfternoonMintime": "14:00",
                "PickupAfternoonMaxtime": "18:00"
            }
        }

        print(values)
        data = json.dumps(values)
        response = requests.post(url, data=data, headers=headers)
        response_dict = json.loads(response.text)
        print(response_dict)
        return response_dict

    @classmethod
    def cancel_pickup(cls, courier_name: str, courier_service_code: int, confirmation_code: str, book_timestamp: int) -> dict:
        """
        取消预约上门取件
        :param courier_name: 承运商名称
        :param courier_service_code: ShipPyPro中承运商服务编码
        :param confirmation_code: ShipPyPro中确认预约单号
        :param book_timestamp:预约时间（时间戳）
        """
        values = {
            "Method": "CancelPickup",
            "Params": {
                "CarrierName": courier_name,
                "CarrierID": courier_service_code,
                "ConfirmationNumber": confirmation_code,
                "PickupTime": book_timestamp
            }
        }
        print(values)
        data = json.dumps(values)
        response = requests.post(url, data=data, headers=headers)
        response_dict = json.loads(response.text)
        print(response_dict)
        return response_dict
